<html>
<head><meta charset="utf-8"><title>safe(r) way to access private fields? · general · Zulip Chat Archive</title></head>
<h2>Stream: <a href="https://rust-lang.github.io/zulip_archive/stream/122651-general/index.html">general</a></h2>
<h3>Topic: <a href="https://rust-lang.github.io/zulip_archive/stream/122651-general/topic/safe(r).20way.20to.20access.20private.20fields.3F.html">safe(r) way to access private fields?</a></h3>

<hr>

<base href="https://rust-lang.zulipchat.com">

<head><link href="https://rust-lang.github.io/zulip_archive/style.css" rel="stylesheet"></head>

<a name="209247563"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/122651-general/topic/safe%28r%29%20way%20to%20access%20private%20fields%3F/near/209247563" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Noah Lev <a href="https://rust-lang.github.io/zulip_archive/stream/122651-general/topic/safe(r).20way.20to.20access.20private.20fields.3F.html#209247563">(Sep 07 2020 at 00:04)</a>:</h4>
<p>Is there any "safe" way to access private fields without using <code>mem::transmute</code>? I want to access the private fields of a <code>regex::Regex</code> so that I can generate Rust code that initializes it with static values.</p>



<a name="209247700"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/122651-general/topic/safe%28r%29%20way%20to%20access%20private%20fields%3F/near/209247700" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> simulacrum <a href="https://rust-lang.github.io/zulip_archive/stream/122651-general/topic/safe(r).20way.20to.20access.20private.20fields.3F.html#209247700">(Sep 07 2020 at 00:09)</a>:</h4>
<p>No, and mem::transmute is not sound to use on repr(Rust) types. I recommend speaking to <span class="user-mention" data-user-id="222471">@BurntSushi</span> and seeing about providing constructors or similar to do what you want.</p>



<a name="209247762"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/122651-general/topic/safe%28r%29%20way%20to%20access%20private%20fields%3F/near/209247762" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Noah Lev <a href="https://rust-lang.github.io/zulip_archive/stream/122651-general/topic/safe(r).20way.20to.20access.20private.20fields.3F.html#209247762">(Sep 07 2020 at 00:11)</a>:</h4>
<p>Okay, thanks!</p>



<a name="209247812"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/122651-general/topic/safe%28r%29%20way%20to%20access%20private%20fields%3F/near/209247812" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Jubilee <a href="https://rust-lang.github.io/zulip_archive/stream/122651-general/topic/safe(r).20way.20to.20access.20private.20fields.3F.html#209247812">(Sep 07 2020 at 00:12)</a>:</h4>
<p>regex::Regex already has a <code>new</code> constructor, what you want to do is, since you know the value ahead of time, use a <code>lazy_static!</code> or OnceCell-based initialization of it, or fork the crate and make <code>new</code> into a const fn. Using the first method (since uh, forking a crate is a bit much...) will still impose a runtime cost on the first initialization, but you will be able to guarantee, using a lazy initializer, that you mostly only pay the cost once.</p>



<a name="209307516"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/122651-general/topic/safe%28r%29%20way%20to%20access%20private%20fields%3F/near/209307516" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Josh Triplett <a href="https://rust-lang.github.io/zulip_archive/stream/122651-general/topic/safe(r).20way.20to.20access.20private.20fields.3F.html#209307516">(Sep 07 2020 at 15:30)</a>:</h4>
<p>Making the function const seems like the best option, if there's enough const eval support to do everything it wants to do. I'm sure there will be eventually.</p>



<a name="209331545"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/122651-general/topic/safe%28r%29%20way%20to%20access%20private%20fields%3F/near/209331545" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Jubilee <a href="https://rust-lang.github.io/zulip_archive/stream/122651-general/topic/safe(r).20way.20to.20access.20private.20fields.3F.html#209331545">(Sep 07 2020 at 22:35)</a>:</h4>
<p>That crate documents a conservative attitude towards bumping MSRV but maybe the maintainer would be inclined to lean more towards doing so if you filed an issue about how the usability of a <code>const new</code> constructor would significantly impact your use of the crate, <span class="user-mention" data-user-id="307537">@Camelid</span>.</p>



<a name="209339181"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/122651-general/topic/safe%28r%29%20way%20to%20access%20private%20fields%3F/near/209339181" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Noah Lev <a href="https://rust-lang.github.io/zulip_archive/stream/122651-general/topic/safe(r).20way.20to.20access.20private.20fields.3F.html#209339181">(Sep 08 2020 at 01:50)</a>:</h4>
<p>It's not a big deal, it was just an idea I had that would require constructing a regex in a proc macro and then serializing the contents of it so it can be reconstructed at runtime without re-parsing the regex. Although it would be cool to get it working!</p>



<hr><p>Last updated: Aug 07 2021 at 22:04 UTC</p>
</html>